﻿// Copyright 2013 Ronald Schlenker, www.technewlogic.de.
// 
// This file is part of Byond.
// 
// Byond is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
// 
// Byond is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Byond.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Linq;

namespace Byond
{
	public static class Extensions
	{
		//public static string GetFullTypeName(this string fileName, string rootPath)
		//{
		//    var nameSpace = GetNameSpace(fileName, rootPath);
		//    return string.IsNullOrWhiteSpace(nameSpace)
		//        ? GetClassName(fileName)
		//        : nameSpace + "." + GetClassName(fileName);
		//}

		public static string GetClassName(this string fileName)
		{
			var possibleClassName = Path.GetFileNameWithoutExtension(fileName);
			if (!CodeGenerator.IsValidLanguageIndependentIdentifier(possibleClassName))
				throw new Exception("'" + possibleClassName + "' is not a valid class name.");

			return possibleClassName;
		}

		public static string GetNameSpace(this string fileName, string rootPath)
		{
			var tmp = "Root" + Path.GetFileNameWithoutExtension(fileName.SubtractLeftPath(rootPath).Replace("\\", "."));
			return tmp.Replace("-", "__").Replace(".", "._");
		}

		//public static string GetLastPathNode(this string path)
		//{
		//    var lastDotIndex = path.LastIndexOf(".");
		//    return lastDotIndex > -1
		//        ? path.Substring(lastDotIndex + 1)
		//        : path;
		//}

		public static string SubtractLeftPath(this string path, string summand)
		{
			if (path.StartsWith(summand))
				path = path.Substring(summand.Length);

			return path.StartsWith(".")
				? path.Remove(0, 1)
				: path;
		}

		//public static string SubtractRightPath(this string path, string summand)
		//{
		//    if (path.EndsWith(path))
		//        path = path.Substring(0, path.Length - summand.Length);

		//    return path.EndsWith(".")
		//        ? path.Substring(0, path.Length - 1)
		//        : path;
		//}

		public static bool TryGetBindingIndex(
			this string bindingExpression,
			out int? index,
			out string bindingExpressionWithoutIndex)
		{
			index = null;
			bindingExpressionWithoutIndex = bindingExpression;

			if (!bindingExpression.EndsWith("]"))
				return false;

			var reverted = bindingExpression
				.ToCharArray()
				.Reverse()
				.Skip(1);
			var numberFound = string.Empty;
			var i = 1;
			foreach (var c in reverted)
			{
				i++;

				if (char.IsDigit(c))
				{
					numberFound = c + numberFound;
					continue;
				}

				if (c == '[' && numberFound != string.Empty)
				{
					int parsedIndex;
					if (int.TryParse(numberFound, out parsedIndex))
					{
						index = parsedIndex;
						bindingExpressionWithoutIndex = bindingExpression
							.Substring(0, bindingExpression.Length - i);
						return true;
					}
					return false;
				}
			}

			return false;
		}
	}
}
